package com.gxuwz.stats.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.stats.entity.*;
import com.gxuwz.stats.service.*;
import com.gxuwz.stats.utils.FormulaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/scoreManage")
public class ScoreManageController {

    @Autowired
    private ResearchProjectScoreService projectScoreService;

    @Autowired
    private AcademicWorkScoreService academicWorkScoreService;

    @Autowired
    private DecisionConsultingScoreService decisionConsultingScoreService;

    @Autowired
    private CreativeAchievementScoreService creativeAchievementScoreService;

    @Autowired
    private IntellectualPropertyScoreService intellectualPropertyScoreService;

    @Autowired
    private AchievementTransferScoreService achievementTransferScoreService;

    @Autowired
    private HumanResearchAwardScoreService humanResearchAwardScoreService;

    @Autowired
    private NatureResearchAwardScoreService natureResearchAwardScoreService;

    @Autowired
    private NetworkCultureScoreService networkCultureScoreService;

    @Autowired
    private AcademicPaperScoreService academicPaperScoreService;

    @Autowired
    private ScientificActivityScoreService scientificActivityScoreService;

    @Autowired
    private ResearchPlatformScoreService researchPlatformScoreService;



    // 科研项目工作量
    @GetMapping("/project")
    public ResponseEntity<Map<String, Object>> getProjectScore(
            @RequestParam Map<String, String> params,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            // 创建 MyBatis Plus 的 Page 对象进行分页查询
            IPage<ResearchProjectScore> page = new Page<>(currentPage, pageSize);
            QueryWrapper<ResearchProjectScore> queryWrapper = new QueryWrapper<>();

            // 其他查询条件
            if (params.containsKey("type") && !params.get("type").isEmpty()) {
                queryWrapper.like("type", params.get("type"));
            }

            if (params.containsKey("status") && !params.get("status").isEmpty()) {
                queryWrapper.eq("status", params.get("status"));
            }

            // 调用 service 层的分页查询方法
            IPage<ResearchProjectScore> resultPage = projectScoreService.page(page, queryWrapper);

            // 构造返回结果
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", resultPage.getRecords()); // 当前页数据
            response.put("total", resultPage.getTotal()); // 总记录数
            response.put("currentPage", resultPage.getCurrent()); // 当前页码
            response.put("pageSize", resultPage.getSize()); // 每页大小
            response.put("totalPages", resultPage.getPages()); // 总页数

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    // 学术著作
    @GetMapping("/academicWork")
    public ResponseEntity<Map<String, Object>> getAcademicWorkScore(
            @RequestParam Map<String, String> params,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            // 创建 MyBatis Plus 的 Page 对象进行分页查询
            IPage<AcademicWorkScore> page = new Page<>(currentPage, pageSize);
            QueryWrapper<AcademicWorkScore> queryWrapper = new QueryWrapper<>();

            // 其他查询条件
            if (params.containsKey("type") && !params.get("type").isEmpty()) {
                queryWrapper.like("type", params.get("type"));
            }

            // 调用 service 层的分页查询方法
            IPage<AcademicWorkScore> resultPage = academicWorkScoreService.page(page, queryWrapper);

            // 构造返回结果
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", resultPage.getRecords()); // 当前页数据
            response.put("total", resultPage.getTotal()); // 总记录数
            response.put("currentPage", resultPage.getCurrent()); // 当前页码
            response.put("pageSize", resultPage.getSize()); // 每页大小
            response.put("totalPages", resultPage.getPages()); // 总页数

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    // 决策咨询类
    @GetMapping("/decisionConsulting")
    public ResponseEntity<Map<String, Object>> getDecisionConsultingScore(
            @RequestParam Map<String, String> params,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            // 创建 MyBatis Plus 的 Page 对象进行分页查询
            IPage<DecisionConsultingScore> page = new Page<>(currentPage, pageSize);
            QueryWrapper<DecisionConsultingScore> queryWrapper = new QueryWrapper<>();

            // 其他查询条件
            if (params.containsKey("type") && !params.get("type").isEmpty()) {
                queryWrapper.like("type", params.get("type"));
            }

            // 调用 service 层的分页查询方法
            IPage<DecisionConsultingScore> resultPage = decisionConsultingScoreService.page(page, queryWrapper);

            // 构造返回结果
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", resultPage.getRecords()); // 当前页数据
            response.put("total", resultPage.getTotal()); // 总记录数
            response.put("currentPage", resultPage.getCurrent()); // 当前页码
            response.put("pageSize", resultPage.getSize()); // 每页大小
            response.put("totalPages", resultPage.getPages()); // 总页数

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    // 创作成果类
    @GetMapping("/creativeAchievement")
    public ResponseEntity<Map<String, Object>> getCreativeAchievementScore(
            @RequestParam Map<String, String> params,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            // 创建 MyBatis Plus 的 Page 对象进行分页查询
            IPage<CreativeAchievementScore> page = new Page<>(currentPage, pageSize);
            QueryWrapper<CreativeAchievementScore> queryWrapper = new QueryWrapper<>();

            // 其他查询条件
            if (params.containsKey("type") && !params.get("type").isEmpty()) {
                queryWrapper.like("type", params.get("type"));
            }

            if (params.containsKey("level") && !params.get("level").isEmpty()) {
                queryWrapper.like("level", params.get("level"));
            }

            // 调用 service 层的分页查询方法
            IPage<CreativeAchievementScore> resultPage = creativeAchievementScoreService.page(page, queryWrapper);

            // 构造返回结果
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", resultPage.getRecords()); // 当前页数据
            response.put("total", resultPage.getTotal()); // 总记录数
            response.put("currentPage", resultPage.getCurrent()); // 当前页码
            response.put("pageSize", resultPage.getSize()); // 每页大小
            response.put("totalPages", resultPage.getPages()); // 总页数

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }


    // 知识产权类
    @GetMapping("/intellectualProperty")
    public ResponseEntity<Map<String, Object>> getIntellectualPropertyScore(
            @RequestParam Map<String, String> params,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            // 创建 MyBatis Plus 的 Page 对象进行分页查询
            IPage<IntellectualPropertyScore> page = new Page<>(currentPage, pageSize);
            QueryWrapper<IntellectualPropertyScore> queryWrapper = new QueryWrapper<>();

            // 其他查询条件
            if (params.containsKey("type") && !params.get("type").isEmpty()) {
                queryWrapper.like("type", params.get("type"));
            }

            // 调用 service 层的分页查询方法
            IPage<IntellectualPropertyScore> resultPage = intellectualPropertyScoreService.page(page, queryWrapper);

            // 构造返回结果
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", resultPage.getRecords()); // 当前页数据
            response.put("total", resultPage.getTotal()); // 总记录数
            response.put("currentPage", resultPage.getCurrent()); // 当前页码
            response.put("pageSize", resultPage.getSize()); // 每页大小
            response.put("totalPages", resultPage.getPages()); // 总页数

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    // 成果转化类
    @GetMapping("/achievementTransfer")
    public ResponseEntity<Map<String, Object>> getAchievementTransferScore(
            @RequestParam Map<String, String> params,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            // 创建 MyBatis Plus 的 Page 对象进行分页查询
            IPage<AchievementTransferScore> page = new Page<>(currentPage, pageSize);
            QueryWrapper<AchievementTransferScore> queryWrapper = new QueryWrapper<>();

            // 其他查询条件
            if (params.containsKey("type") && !params.get("type").isEmpty()) {
                queryWrapper.like("type", params.get("type"));
            }

            // 调用 service 层的分页查询方法
            IPage<AchievementTransferScore> resultPage = achievementTransferScoreService.page(page, queryWrapper);

            // 构造返回结果
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", resultPage.getRecords()); // 当前页数据
            response.put("total", resultPage.getTotal()); // 总记录数
            response.put("currentPage", resultPage.getCurrent()); // 当前页码
            response.put("pageSize", resultPage.getSize()); // 每页大小
            response.put("totalPages", resultPage.getPages()); // 总页数

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }


    // 科研奖项（人文社科）
    @GetMapping("/humanResearchAward")
    public ResponseEntity<Map<String, Object>> getHumanResearchAwardScore(
            @RequestParam Map<String, String> params,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            // 创建 MyBatis Plus 的 Page 对象进行分页查询
            IPage<HumanResearchAwardScore> page = new Page<>(currentPage, pageSize);
            QueryWrapper<HumanResearchAwardScore> queryWrapper = new QueryWrapper<>();

            // 其他查询条件
            if (params.containsKey("type") && !params.get("type").isEmpty()) {
                queryWrapper.like("type", params.get("type"));
            }

            // 调用 service 层的分页查询方法
            IPage<HumanResearchAwardScore> resultPage = humanResearchAwardScoreService.page(page, queryWrapper);

            // 构造返回结果
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", resultPage.getRecords()); // 当前页数据
            response.put("total", resultPage.getTotal()); // 总记录数
            response.put("currentPage", resultPage.getCurrent()); // 当前页码
            response.put("pageSize", resultPage.getSize()); // 每页大小
            response.put("totalPages", resultPage.getPages()); // 总页数

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    // 科研奖项（自然科学）
    @GetMapping("/natureResearchAward")
    public ResponseEntity<Map<String, Object>> getNatureResearchAwardScore(
            @RequestParam Map<String, String> params,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            // 创建 MyBatis Plus 的 Page 对象进行分页查询
            IPage<NatureResearchAwardScore> page = new Page<>(currentPage, pageSize);
            QueryWrapper<NatureResearchAwardScore> queryWrapper = new QueryWrapper<>();

            // 其他查询条件
            if (params.containsKey("type") && !params.get("type").isEmpty()) {
                queryWrapper.like("type", params.get("type"));
            }

            // 调用 service 层的分页查询方法
            IPage<NatureResearchAwardScore> resultPage = natureResearchAwardScoreService.page(page, queryWrapper);

            // 构造返回结果
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", resultPage.getRecords()); // 当前页数据
            response.put("total", resultPage.getTotal()); // 总记录数
            response.put("currentPage", resultPage.getCurrent()); // 当前页码
            response.put("pageSize", resultPage.getSize()); // 每页大小
            response.put("totalPages", resultPage.getPages()); // 总页数

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    // 网络文化成果
    @GetMapping("/networkCulture")
    public ResponseEntity<Map<String, Object>> getNetworkCultureScore(
            @RequestParam Map<String, String> params,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            // 创建 MyBatis Plus 的 Page 对象进行分页查询
            IPage<NetworkCultureScore> page = new Page<>(currentPage, pageSize);
            QueryWrapper<NetworkCultureScore> queryWrapper = new QueryWrapper<>();

            // 其他查询条件
            if (params.containsKey("type") && !params.get("type").isEmpty()) {
                queryWrapper.like("type", params.get("type"));
            }

            // 调用 service 层的分页查询方法
            IPage<NetworkCultureScore> resultPage = networkCultureScoreService.page(page, queryWrapper);

            // 构造返回结果
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", resultPage.getRecords()); // 当前页数据
            response.put("total", resultPage.getTotal()); // 总记录数
            response.put("currentPage", resultPage.getCurrent()); // 当前页码
            response.put("pageSize", resultPage.getSize()); // 每页大小
            response.put("totalPages", resultPage.getPages()); // 总页数

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    // 学术论文
    @GetMapping("/academicPaper")
    public ResponseEntity<Map<String, Object>> getAcademicPaperScore(
            @RequestParam Map<String, String> params,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            // 创建 MyBatis Plus 的 Page 对象进行分页查询
            IPage<AcademicPaperScore> page = new Page<>(currentPage, pageSize);
            QueryWrapper<AcademicPaperScore> queryWrapper = new QueryWrapper<>();

            // 其他查询条件
            if (params.containsKey("type") && !params.get("type").isEmpty()) {
                queryWrapper.like("type", params.get("type"));
            }

            // 调用 service 层的分页查询方法
            IPage<AcademicPaperScore> resultPage = academicPaperScoreService.page(page, queryWrapper);

            // 构造返回结果
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", resultPage.getRecords()); // 当前页数据
            response.put("total", resultPage.getTotal()); // 总记录数
            response.put("currentPage", resultPage.getCurrent()); // 当前页码
            response.put("pageSize", resultPage.getSize()); // 每页大小
            response.put("totalPages", resultPage.getPages()); // 总页数

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    // 科技活动
    @GetMapping("/scientificActivity")
    public ResponseEntity<Map<String, Object>> getScientificActivityScore(
            @RequestParam Map<String, String> params,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            // 创建 MyBatis Plus 的 Page 对象进行分页查询
            IPage<ScientificActivityScore> page = new Page<>(currentPage, pageSize);
            QueryWrapper<ScientificActivityScore> queryWrapper = new QueryWrapper<>();

            // 其他查询条件
            if (params.containsKey("type") && !params.get("type").isEmpty()) {
                queryWrapper.like("type", params.get("type"));
            }

            // 调用 service 层的分页查询方法
            IPage<ScientificActivityScore> resultPage = scientificActivityScoreService.page(page, queryWrapper);

            // 构造返回结果
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", resultPage.getRecords()); // 当前页数据
            response.put("total", resultPage.getTotal()); // 总记录数
            response.put("currentPage", resultPage.getCurrent()); // 当前页码
            response.put("pageSize", resultPage.getSize()); // 每页大小
            response.put("totalPages", resultPage.getPages()); // 总页数

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    // 重要平台
    @GetMapping("/researchPlatform")
    public ResponseEntity<Map<String, Object>> getResearchPlatformScore(
            @RequestParam Map<String, String> params,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            // 创建 MyBatis Plus 的 Page 对象进行分页查询
            IPage<ResearchPlatformScore> page = new Page<>(currentPage, pageSize);
            QueryWrapper<ResearchPlatformScore> queryWrapper = new QueryWrapper<>();

            // 其他查询条件
            if (params.containsKey("type") && !params.get("type").isEmpty()) {
                queryWrapper.like("type", params.get("type"));
            }

            // 调用 service 层的分页查询方法
            IPage<ResearchPlatformScore> resultPage = researchPlatformScoreService.page(page, queryWrapper);

            // 构造返回结果
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", resultPage.getRecords()); // 当前页数据
            response.put("total", resultPage.getTotal()); // 总记录数
            response.put("currentPage", resultPage.getCurrent()); // 当前页码
            response.put("pageSize", resultPage.getSize()); // 每页大小
            response.put("totalPages", resultPage.getPages()); // 总页数

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    // 获取所有类型的分值公式
    @GetMapping("/all")
    public ResponseEntity<Map<String, Object>> getAll() {
        Map<String, Object> response = new HashMap<>();
        try {
            List<AchievementTransferScore> list = achievementTransferScoreService.getAll();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", list);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            response.put("code", 500);
            response.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    // 更新某个类型的分值公式
    @PostMapping("/update")
    public ResponseEntity<Map<String, Object>> update(@RequestBody AchievementTransferScore score) {
        Map<String, Object> response = new HashMap<>();
        try {
            achievementTransferScoreService.updateByType(score.getType(), score.getScoreFormula(), score.getVariables());
            response.put("code", 200);
            response.put("message", "更新成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            response.put("code", 500);
            response.put("message", "更新失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    // 计算分值
    @PostMapping("/compute")
    public ResponseEntity<Map<String, Object>> compute(@RequestBody Map<String, Object> params) {
        Map<String, Object> response = new HashMap<>();
        try {
            String type = (String) params.get("type");
            AchievementTransferScore scoreObj = achievementTransferScoreService.getByType(type);
            if (scoreObj == null) {
                response.put("code", 404);
                response.put("message", "未配置该类型的分值公式");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
            }
            String formula = scoreObj.getScoreFormula();
            String[] variables = scoreObj.getVariables().split(",");
            Map<String, Object> vars = new HashMap<>();
            for (String var : variables) {
                if (params.containsKey(var)) {
                    vars.put(var, params.get(var));
                }
            }
            Object score = FormulaUtil.eval(formula, vars);
            System.out.println("计算后的分值="+score);
            response.put("code", 200);
            response.put("message", "计算成功");
            response.put("data", score);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            response.put("code", 500);
            response.put("message", "计算失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    // 添加分值
    @PostMapping("/{category}")
    public ResponseEntity<Map<String, Object>> addScore(
            @PathVariable String category,
            @RequestBody Map<String, Object> scoreData) {
        try {
            Map<String, Object> response = new HashMap<>();
            
            switch (category) {
                case "project":
                    ResearchProjectScore projectScore = new ResearchProjectScore();
                    projectScore.setType((String) scoreData.get("type"));
                    projectScore.setType((String) scoreData.get("status"));
                    projectScore.setScore(((Number) scoreData.get("score")).intValue());
                    projectScoreService.save(projectScore);
                    break;
                case "academicWork":
                    AcademicWorkScore academicWorkScore = new AcademicWorkScore();
                    academicWorkScore.setType((String) scoreData.get("type"));
                    academicWorkScore.setScore(((Number) scoreData.get("score")).intValue());
                    academicWorkScoreService.save(academicWorkScore);
                    break;
                case "decisionConsulting":
                    DecisionConsultingScore decisionConsultingScore = new DecisionConsultingScore();
                    decisionConsultingScore.setType((String) scoreData.get("type"));
                    decisionConsultingScore.setScore(((Number) scoreData.get("score")).intValue());
                    decisionConsultingScoreService.save(decisionConsultingScore);
                    break;
                case "creativeAchievement":
                    CreativeAchievementScore creativeScore = new CreativeAchievementScore();
                    creativeScore.setType((String) scoreData.get("type"));
                    creativeScore.setLevel((String) scoreData.get("level"));
                    creativeScore.setScore(((Number) scoreData.get("score")).intValue());
                    creativeAchievementScoreService.save(creativeScore);
                    break;
                case "intellectualProperty":
                    IntellectualPropertyScore intellectualPropertyScore = new IntellectualPropertyScore();
                    intellectualPropertyScore.setType((String) scoreData.get("type"));
                    intellectualPropertyScore.setScore(((Number) scoreData.get("score")).intValue());
                    intellectualPropertyScoreService.save(intellectualPropertyScore);
                    break;
                case "achievementTransfer":
                    AchievementTransferScore achievementTransferScore = new AchievementTransferScore();
                    achievementTransferScore.setType((String) scoreData.get("type"));
//                    achievementTransferScore.setScore(((Number) scoreData.get("score")).intValue());
                    achievementTransferScoreService.save(achievementTransferScore);
                    break;
                case "humanResearchAward":
                    HumanResearchAwardScore humanResearchAwardScore = new HumanResearchAwardScore();
                    humanResearchAwardScore.setType((String) scoreData.get("type"));
                    humanResearchAwardScore.setScore(((Number) scoreData.get("score")).intValue());
                    humanResearchAwardScoreService.save(humanResearchAwardScore);
                    break;
                case "natureResearchAward":
                    NatureResearchAwardScore natureResearchAwardScore = new NatureResearchAwardScore();
                    natureResearchAwardScore.setType((String) scoreData.get("type"));
                    natureResearchAwardScore.setScore(((Number) scoreData.get("score")).intValue());
                    natureResearchAwardScoreService.save(natureResearchAwardScore);
                    break;
                case "networkCulture":
                    NetworkCultureScore networkCultureScore = new NetworkCultureScore();
                    networkCultureScore.setType((String) scoreData.get("type"));
                    networkCultureScore.setScore(((Number) scoreData.get("score")).intValue());
                    networkCultureScoreService.save(networkCultureScore);
                    break;
                case "academicPaper":
                    AcademicPaperScore academicPaperScore = new AcademicPaperScore();
                    academicPaperScore.setType((String) scoreData.get("type"));
                    academicPaperScore.setScore(((Number) scoreData.get("score")).intValue());
                    academicPaperScoreService.save(academicPaperScore);
                    break;
                case "scientificActivity":
                    ScientificActivityScore scientificActivityScore = new ScientificActivityScore();
                    scientificActivityScore.setType((String) scoreData.get("type"));
                    scientificActivityScore.setScore(((Number) scoreData.get("score")).intValue());
                    scientificActivityScoreService.save(scientificActivityScore);
                    break;
                case "researchPlatform":
                    ResearchPlatformScore researchPlatformScore = new ResearchPlatformScore();
                    researchPlatformScore.setType((String) scoreData.get("type"));
                    researchPlatformScore.setScore(((Number) scoreData.get("score")).intValue());
                    researchPlatformScoreService.save(researchPlatformScore);
                    break;
                default:
                    throw new IllegalArgumentException("未知的分类: " + category);
            }
            
            response.put("code", 200);
            response.put("message", "添加成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "添加失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    // 修改分值
    @PutMapping("/{category}")
    public ResponseEntity<Map<String, Object>> updateScore(
            @PathVariable String category,
            @RequestBody Map<String, Object> scoreData) {
        try {
            Map<String, Object> response = new HashMap<>();
            
            switch (category) {
                case "project":
                    ResearchProjectScore projectScore = new ResearchProjectScore();
                    projectScore.setId(((Number) scoreData.get("id")).intValue());
                    projectScore.setType((String) scoreData.get("type"));
                    projectScore.setType((String) scoreData.get("status"));
                    projectScore.setScore(((Number) scoreData.get("score")).intValue());
                    projectScoreService.updateById(projectScore);
                    break;
                case "academicWork":
                    AcademicWorkScore academicWorkScore = new AcademicWorkScore();
                    academicWorkScore.setId(((Number) scoreData.get("id")).intValue());
                    academicWorkScore.setType((String) scoreData.get("type"));
                    academicWorkScore.setScore(((Number) scoreData.get("score")).intValue());
                    academicWorkScoreService.updateById(academicWorkScore);
                    break;
                case "decisionConsulting":
                    DecisionConsultingScore decisionConsultingScore = new DecisionConsultingScore();
                    decisionConsultingScore.setId(((Number) scoreData.get("id")).intValue());
                    decisionConsultingScore.setType((String) scoreData.get("type"));
                    decisionConsultingScore.setScore(((Number) scoreData.get("score")).intValue());
                    decisionConsultingScoreService.updateById(decisionConsultingScore);
                    break;
                case "creativeAchievement":
                    CreativeAchievementScore creativeScore = new CreativeAchievementScore();
                    creativeScore.setId(((Number) scoreData.get("id")).intValue());
                    creativeScore.setType((String) scoreData.get("type"));
                    creativeScore.setLevel((String) scoreData.get("level"));
                    creativeScore.setScore(((Number) scoreData.get("score")).intValue());
                    creativeAchievementScoreService.updateById(creativeScore);
                    break;
                case "intellectualProperty":
                    IntellectualPropertyScore intellectualPropertyScore = new IntellectualPropertyScore();
                    intellectualPropertyScore.setId(((Number) scoreData.get("id")).intValue());
                    intellectualPropertyScore.setType((String) scoreData.get("type"));
                    intellectualPropertyScore.setScore(((Number) scoreData.get("score")).intValue());
                    intellectualPropertyScoreService.updateById(intellectualPropertyScore);
                    break;
                case "achievementTransfer":
                    AchievementTransferScore achievementTransferScore = new AchievementTransferScore();
                    achievementTransferScore.setId(((Number) scoreData.get("id")).intValue());
                    achievementTransferScore.setType((String) scoreData.get("type"));
//                    achievementTransferScore.setScore(((Number) scoreData.get("score")).intValue());
                    achievementTransferScoreService.updateById(achievementTransferScore);
                    break;
                case "humanResearchAward":
                    HumanResearchAwardScore humanResearchAwardScore = new HumanResearchAwardScore();
                    humanResearchAwardScore.setId(((Number) scoreData.get("id")).intValue());
                    humanResearchAwardScore.setType((String) scoreData.get("type"));
                    humanResearchAwardScore.setScore(((Number) scoreData.get("score")).intValue());
                    humanResearchAwardScoreService.updateById(humanResearchAwardScore);
                    break;
                case "natureResearchAward":
                    NatureResearchAwardScore natureResearchAwardScore = new NatureResearchAwardScore();
                    natureResearchAwardScore.setId(((Number) scoreData.get("id")).intValue());
                    natureResearchAwardScore.setType((String) scoreData.get("type"));
                    natureResearchAwardScore.setScore(((Number) scoreData.get("score")).intValue());
                    natureResearchAwardScoreService.updateById(natureResearchAwardScore);
                    break;
                case "networkCulture":
                    NetworkCultureScore networkCultureScore = new NetworkCultureScore();
                    networkCultureScore.setId(((Number) scoreData.get("id")).intValue());
                    networkCultureScore.setType((String) scoreData.get("type"));
                    networkCultureScore.setScore(((Number) scoreData.get("score")).intValue());
                    networkCultureScoreService.updateById(networkCultureScore);
                    break;
                case "academicPaper":
                    AcademicPaperScore academicPaperScore = new AcademicPaperScore();
                    academicPaperScore.setId(((Number) scoreData.get("id")).intValue());
                    academicPaperScore.setType((String) scoreData.get("type"));
                    academicPaperScore.setScore(((Number) scoreData.get("score")).intValue());
                    academicPaperScoreService.updateById(academicPaperScore);
                    break;
                case "scientificActivity":
                    ScientificActivityScore scientificActivityScore = new ScientificActivityScore();
                    scientificActivityScore.setId(((Number) scoreData.get("id")).intValue());
                    scientificActivityScore.setType((String) scoreData.get("type"));
                    scientificActivityScore.setScore(((Number) scoreData.get("score")).intValue());
                    scientificActivityScoreService.updateById(scientificActivityScore);
                    break;
                case "researchPlatform":
                    ResearchPlatformScore researchPlatformScore = new ResearchPlatformScore();
                    researchPlatformScore.setId(((Number) scoreData.get("id")).intValue());
                    researchPlatformScore.setType((String) scoreData.get("type"));
                    researchPlatformScore.setScore(((Number) scoreData.get("score")).intValue());
                    researchPlatformScoreService.updateById(researchPlatformScore);
                    break;
                default:
                    throw new IllegalArgumentException("未知的分类: " + category);
            }
            
            response.put("code", 200);
            response.put("message", "修改成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "修改失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

    // 删除分值
    @DeleteMapping("/{category}")
    public ResponseEntity<Map<String, Object>> deleteScore(
            @PathVariable String category,
            @RequestBody List<Number> ids) {
        try {
            Map<String, Object> response = new HashMap<>();
            List<Integer> intIds = ids.stream()
                    .map(Number::intValue)
                    .collect(Collectors.toList());
            
            switch (category) {
                case "project":
                    projectScoreService.removeByIds(intIds);
                    break;
                case "academicWork":
                    academicWorkScoreService.removeByIds(intIds);
                    break;
                case "decisionConsulting":
                    decisionConsultingScoreService.removeByIds(intIds);
                    break;
                case "creativeAchievement":
                    creativeAchievementScoreService.removeByIds(intIds);
                    break;
                case "intellectualProperty":
                    intellectualPropertyScoreService.removeByIds(intIds);
                    break;
                case "achievementTransfer":
                    achievementTransferScoreService.removeByIds(intIds);
                    break;
                case "humanResearchAward":
                    humanResearchAwardScoreService.removeByIds(intIds);
                    break;
                case "natureResearchAward":
                    natureResearchAwardScoreService.removeByIds(intIds);
                    break;
                case "networkCulture":
                    networkCultureScoreService.removeByIds(intIds);
                    break;
                case "academicPaper":
                    academicPaperScoreService.removeByIds(intIds);
                    break;
                case "scientificActivity":
                    scientificActivityScoreService.removeByIds(intIds);
                    break;
                case "researchPlatform":
                    researchPlatformScoreService.removeByIds(intIds);
                    break;
                default:
                    throw new IllegalArgumentException("未知的分类: " + category);
            }
            
            response.put("code", 200);
            response.put("message", "删除成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "删除失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
        }
    }

}
